﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Draco.OWF.Studio.Common.Geometry
{
    
    public class GeometryUtils
    {
        //public static Point[] GetIntersection(Point p1, Point p2, Rect rect)
        //{
        //    List<Point> Points = new List<Point>();
        //    PointRectRelation realtion1 = GetPointRectRelationship(p1, rect);
        //    PointRectRelation realtion2 = GetPointRectRelationship(p2, rect);
        //    if (realtion1 == PointRectRelation.On && realtion2 == PointRectRelation.On)
        //    {
        //        return new Point[] { p1, p2 };
        //    }
        //    if (realtion1 == PointRectRelation.On) 
        //        return new Point[]{p1};
        //    if (realtion2 == PointRectRelation.On)
        //        return new Point[] { p2 };

        //    if (realtion1 == PointRectRelation.Inside && realtion2 == PointRectRelation.Inside)
        //        return new Point[0];
            
        //    Point PointInner = IsPointInRect(p1, rect) ? p1 : p2;
        //    Point PointOuter = IsPointInRect(p1, rect) ? p2 : p1;

        //    double TopDelta = PointInner.Y - rect.Top;
        //    double LeftDelta = PointInner.X - rect.Left;
        //    double BottomDelta = rect.Bottom - PointInner.Y;
        //    double RightDelta = rect.Right - PointInner.X;

        //    double theta = Math.Atan2(PointOuter.Y - PointInner.Y, PointOuter.X - PointInner.X);
        //    if (theta >= 0 && theta < 90)
        //    {
        //        //Double X = 
        //    }
        //    if (theta >= 90 && theta < 180)
        //    {
        //    } 
        //    if (theta >= 180 && theta < 270)
        //    { 
        //    }
        //    if (theta >= 270)
        //    {
 
        //    }
        //    double sint = Math.Sin(theta);
        //    double cost = Math.Cos(theta);
        //    return new Point[]{};
        //}

        

        #region relationship of LineSeg
        // 如果线段u和v相交(包括相交在端点处)时，返回true 
        public static bool Intersect(LineSeg u, LineSeg v)
        {
            return 
            ((Math.Max(u.Start.X, u.End.X) >= Math.Min(v.Start.X, v.End.X)) && //排斥实验 
            (Math.Max(v.Start.X, v.End.X) >= Math.Min(u.Start.X, u.End.X)) &&
            (Math.Max(u.Start.Y, u.End.Y) >= Math.Min(v.Start.Y, v.End.Y)) &&
            (Math.Max(v.Start.Y, v.End.Y) >= Math.Min(u.Start.Y, u.End.Y)) &&
            (Multiply(v.Start, u.End, u.Start) * Multiply(u.End, v.End, u.Start) >= 0) && //跨立实验 
            (Multiply(u.Start, v.End, v.Start) * Multiply(v.End, u.End, v.Start) >= 0));
        }
        /****************************************************************************** 
        r=multiply(sp,ep,op),得到(sp-op)*(ep-op)的叉积 
        r>0:ep在矢量opsp的逆时针方向； 
        r=0：opspep三点共线； 
        r<0:ep在矢量opsp的顺时针方向 
        *******************************************************************************/
        private static double Multiply(Point sp, Point ep, Point op)
        {
            return ((sp.X - op.X) * (ep.Y - op.Y) - (ep.X - op.X) * (sp.Y - op.Y));
        }
        #endregion
    }
}
